package org.infodavid.common.model.impl;

import java.beans.PropertyChangeEvent;
import java.lang.reflect.Field;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * The Class PoolablePropertyChangeEvent.
 */
public class PoolablePropertyChangeEvent extends PropertyChangeEvent {

	/** serialVersionUID. */
	private static final long serialVersionUID = 9108168933947191947L;

	/** logger. */
	private static final Log LOG = LogFactory.getLog(PoolablePropertyChangeEvent.class);

	/**
	 * The Constructor.
	 */
	public PoolablePropertyChangeEvent() {
		super("Unknown", "Unknown", null, null);
	}

	/**
	 * Inits the.
	 */
	public void init() {
		setPropagationId(null);
		setPropertyName(null);
		setNewValue(null);
		setOldValue(null);
		setSource(null);
	}

	/**
	 * Sets the property name.
	 * @param value the property name
	 */
	public void setPropertyName(final String value) {
		try {
			final Field field = PropertyChangeEvent.class.getDeclaredField("propertyName");

			field.setAccessible(true);
			field.set(this, value);
		}
		catch (final Exception e) {
			LOG.error("An error occurs while setting property name", e);

			throw new IllegalAccessError(e.getMessage());
		}
	}

	/**
	 * Sets the new value.
	 * @param value the new value
	 */
	public void setNewValue(final Object value) {
		try {
			final Field field = PropertyChangeEvent.class.getDeclaredField("newValue");

			field.setAccessible(true);
			field.set(this, value);
		}
		catch (final Exception e) {
			LOG.error("An error occurs while setting property name", e);

			throw new IllegalAccessError(e.getMessage());
		}
	}

	/**
	 * Sets the old value.
	 * @param value the old value
	 */
	public void setOldValue(final Object value) {
		try {
			final Field field = PropertyChangeEvent.class.getDeclaredField("oldValue");

			field.setAccessible(true);
			field.set(this, value);
		}
		catch (final Exception e) {
			LOG.error("An error occurs while setting property name", e);

			throw new IllegalAccessError(e.getMessage());
		}
	}

	/**
	 * Sets the source.
	 * @param value the source
	 */
	public void setSource(final Object value) {
		source = value;
	}
}
